import React from 'react'
import { View, Image, Linking, StatusBar, ScrollView } from 'react-native'
import { Lt, Nav, Dialog, Images, Label, Jpp, Touch, Navigator, Jx, Color } from "../_utils"
import { Net, NET_GET, NET_NR, NET_RI } from "../_net"
import { eApp, cHome } from '../home/_home';
import { Details } from './ApplyDetails';
import { GoodsBottom } from './ApplyGoodsBottom';
import { eShare } from '../_util';

export class ApplyGoodsDetail extends React.PureComponent{
    constructor(props){
        super(props);
        const { data } = this.props.navigation.state.params;
        this.data = data;
        cHome.setImages(data, (init, image, details, images)=>{
            if(init){
                this.state = {image, details, images};
            }else {
                this.setState({image, details, images});
            }
            if(image == details[0]){
                console.warn("第一张图用的是详情图");
            }
        })
        // this.onCoupon = this.onCoupon.bind(this);
        this.onShare = this.onShare.bind(this);
    }
    static navigationOptions = {header:null,}
    // componentDidMount(){
    //     Net.request("score/upVis?goodId="+this.data.goodId, null, {NET_GET, NET_NR}).then(r=>null);
    // }
    componentWillUnmount(){
        if(Jpp.isAndroid){
            StatusBar.setTranslucent(false);
        }
    }
    // fetConvert(data){
    //     if(this.convert){
    //         return Promise.resolve(this.convert);
    //     }
    //     return cHome.toConver(data, r=>this.convert=r);
    // }
    // openURL(url){
    //     Linking.openURL(url).then(r=>{Dialog.release();}).catch(err=>{Dialog.release();});
    // }
    // onCoupon(){
    //     const data = this.data;
    //     this.fetConvert(data).then(r=>{
    //         // r.tkLink
    //         const app = eApp.idxs[data.appIdx];
    //         Dialog.retain(true);
    //         //  || app == eApp.JD
    //         if(Jpp.isAndroid && (app == eApp.PDD || app == eApp.JD)){
    //             this.openURL(r.uland_url);
    //             return;
    //         }
    //         const schurl = eApp.urls[app];
    //         let scheme = cHome.toScheme(app, data.goodId, r);
    //         if(scheme){
    //             scheme = schurl + scheme;
    //             console.warn("跳转: " + scheme);
    //         }
    //         Linking.canOpenURL(schurl).then(supported=>{
    //             if (supported && scheme) {
    //                 this.openURL(scheme);
    //             } else {
    //                 this.openURL(r.uland_url);
    //             }
    //         })
    //     });
    // }
    onShare(){
        const data = this.data;
        const { images } = this.state;
        Navigator.push("ApplyShareTo", { images, eshare:eShare.detail, data, });
        // this.fetConvert(data).then(r=>{
        //     console.warn(Jx.stringify(r));
        //     // isCombine:true, isFromDetail:true
        //     Navigator.push("ShareTo", { images, eshare:eShare.detail, data:{...data, ...r}, });
        // }).catch(err=>{
        //     return;
        //     Navigator.push("ShareTo", { images, isCombine:true, data:{...data, qrcode:Jpp.imageTemp,uland_url:"(服务器超时)"}, });
        // });
    }

    render(){
        const { image, images, details } = this.state;
        const data = this.data;
        return <View style = {{flex:1}}>
            <Nav.StatusBar translucent={true}/>
            <Details
                data = {data} 
                images = {images}
                details = {details}
                // onCoupon = {this.onCoupon}
            />
            <Touch style={{position:"absolute"}} onPress={Navigator.pop.bind(Navigator)}>
                <Image source={Images.back_sp} style={{margin:20, marginTop:44}}/>
            </Touch>
            <GoodsBottom
                data = {data}
                onBuy ={this.onShare}
                onImage = {()=>image}
                onShare = {this.onShare}
            />
        </View>
    }
}
